﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
using System.Data;
namespace WebApplication.Admin
{
    public partial class PhieuChiAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
               
                LoadNhanvien();
                
            }
        }

        public void LoadHienthi()
        {
            string strid = "" + Request["MaPC"];
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            PHIEUCHIRepository repository = new PHIEUCHIRepository(new EFRepository<PHIEUCHI>(), iunitfwork);
            if( !string.IsNullOrEmpty(strid))
            {
                PHIEUCHI objPhieuchi = repository.GetById(strid);
                txtmaphieuchi.Value = objPhieuchi.MaPC;
                Dlnhanvien.SelectedValue = objPhieuchi.MaNV;
                //Dlmahdn.SelectedValue = objPhieuchi.MaHDN;
                txtsotienchi.Value = objPhieuchi.SoTienDaTra_PC.ToString();
                txtnoidung.Value = objPhieuchi.NoiDungChi;
            }
        }
        /// <summary>
        /// Load Nhân viên
        /// </summary>
        public void LoadNhanvien()
        {
            IUnitOfWork iunfwork = new EFUnitOfWork();
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunfwork);
            IQueryable<NHANVIEN> objNhanvien = repository.All();
            Dlnhanvien.DataSource = objNhanvien;
            Dlnhanvien.DataValueField = "MANV";
            Dlnhanvien.DataTextField = "TENNV";
            Dlnhanvien.DataBind();
            Dlnhanvien.Items.Insert(0, new ListItem { Text = "---Chọn nhân viên---", Value = "0" });
            
        }
        /// <summary>
        /// Load Hóa đơn nhập
        /// </summary>
        //public void Loadmathang()
        //{
        //    IUnitOfWork iunitfwork = new EFUnitOfWork();
        //    HOADONNHAPRepository repository=new HOADONNHAPRepository(new EFRepository<HOADONNHAP>(),iunitfwork);
        //    IQueryable<HOADONNHAP> objMathang=repository.All();
        //    Dlmahdn.DataSource = objMathang;
        //    Dlmahdn.DataValueField = "MAHDN";
        //    Dlmahdn.DataTextField = "MAHDN";
        //    Dlmahdn.DataBind();
        //    Dlmahdn.Items.Insert(0, new ListItem { Text = "---Chọn mã hóa đơn---", Value = "0" }); 
        //}
        public void Capnhap()
        {
            string strid = "" + Request["MaPC"];
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            PHIEUCHIRepository repository = new PHIEUCHIRepository(new EFRepository<PHIEUCHI>(), iunitfwork);
            PHIEUCHI objphieuchi = null;
            bool iscapnhap = true;
            if (!string.IsNullOrEmpty(strid))
            {
                iscapnhap = false;
                objphieuchi = repository.GetById(strid);
            }
            else
            {
                iscapnhap = true;
                objphieuchi = new PHIEUCHI();
            }
            if (string.IsNullOrEmpty(txtmaphieuchi.Value) || Dlnhanvien.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập đầy đủ')</script>");
                return;
            }
            objphieuchi.MaNV = Dlnhanvien.SelectedValue;
            objphieuchi.MaPC = txtmaphieuchi.Value;
            
            //if (! Dlmahdn.SelectedValue.Equals("0"))
            //{
            //    objphieuchi.MaHDN = Dlnhanvien.SelectedValue;
            //}
            objphieuchi.NgayChi = DateTime.Now;
            objphieuchi.SoTienDaTra_PC = int.Parse(txtsotienchi.Value);
            objphieuchi.NoiDungChi =txtnoidung.Value;
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objphieuchi);
                }
                repository.Save();
            }
            catch (Exception ex)
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('lỗi rồi')</script>");
            }

        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            Capnhap();
            Phieuchi();
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            txtmaphieuchi.Value = "";
            txtnoidung.Value = "";
            txtsotienchi.Value = "";
            Dlnhanvien.SelectedValue = "0";
            //Dlmahdn.SelectedValue = "0";
            Response.Redirect("PhieuChiAdd.aspx");

        }
        public void Phieuchi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
            NHANVIEN objnhanvien = null;
            if(Dlnhanvien.SelectedValue.Equals("0"))
            {
                string manv=Dlnhanvien.SelectedValue;
                objnhanvien=repository.All().Where(p=>p.MANV==manv).FirstOrDefault();
            }
            DataTable datakh1 = new DataTable();
            DataTable data = new DataTable();
            DataTable datapchi = new DataTable();
            datapchi = (DataTable)Session["phieuchi"];
            data.Columns.Add("mapc", typeof(string));
            //data.Columns.Add("mahdn", typeof(string));
            data.Columns.Add("sotien", typeof(int));
            data.Columns.Add("noidung", typeof(string));
            data.Columns.Add("nhanvien", typeof(string));
            data.Columns.Add("ngaychi", typeof(DateTime));
            if (Session["phieuchi"] == null)
            {
                DataRow rows = data.NewRow();
                rows["mapc"] = txtmaphieuchi.Value;
                //if(! Dlmahdn.SelectedValue.Equals("0"))
                //{
                //    rows["mahdn"] = Dlmahdn.SelectedValue;
                //}
                rows["sotien"] = int.Parse(txtsotienchi.Value);
                rows["noidung"] = txtnoidung.Value;
                if (objnhanvien != null)
                {
                    rows["nhanvien"] = objnhanvien.TENNV;
                }
                rows["ngaychi"] = DateTime.Now;
                data.Rows.Add(rows);
            }
            if (data.Rows.Count > 0)
            {
                Session["phieuchi"] = data;
               
            }
        }
    }
}